package com.cloud.cmt.usermanage.controller;

import com.angela.cloud.common.base.ResultMessage;
import com.angela.cloud.common.constant.ResultCodeConstant;
import com.cloud.cmt.usermanage.base.BaseVaule;
import com.cloud.cmt.usermanage.base.PagePo;
import com.cloud.cmt.usermanage.base.PageRequestFactory;
import com.cloud.cmt.usermanage.entity.Role;
import com.cloud.cmt.usermanage.service.RoleRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * User: wcy
 * Date: 2017/10/17
 * Time: 16:14
 */
@RestController
@RequestMapping("/role")
@Api(value = "/角色管理", description = "角色管理-用户角色增删查改等-by wcy")
public class RoleCtl {

    @Resource
    RoleRepository RoleRepository;

    @ApiOperation(value = "通过id获取角色信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResultMessage<Role> getUser(@PathVariable long id) {
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        resultMessage.setData(RoleRepository.findOne(id));
        return resultMessage;
    }

    @ApiOperation(value = "保存角色信息")
    @PostMapping(value = "")
    @ResponseBody
    public ResultMessage<Role> save(@RequestBody Role Role) {
        ResultMessage resultMessage = new ResultMessage();
        Role = RoleRepository.save(Role);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        resultMessage.setId(Role.getId() + "");

        return resultMessage;
    }

    @ApiOperation(value = "更新角色信息")
    @PutMapping(value = "")
    @ResponseBody
    public ResultMessage<Role> update(@RequestBody Role Role) {
        ResultMessage resultMessage = new ResultMessage();
        RoleRepository.save(Role);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);

        return resultMessage;
    }

    @ApiOperation(value = "查询所有角色")
    @PostMapping(value = "/findPage")
    @ResponseBody
    public ResultMessage<List<Role>> findPage(@RequestBody PagePo<BaseVaule> pagePo) {
        return ResultMessage.success(RoleRepository.getRoles("%" + pagePo.getFields().getValue() + "%", PageRequestFactory.getPageInfo(pagePo)));
    }

    @ApiOperation(value = "通过id删除角色")
    @DeleteMapping(value = "/{id}")
    public ResultMessage remove(@PathVariable("id") Long id) {
        ResultMessage resultMessage = new ResultMessage();
        RoleRepository.delete(id);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        return resultMessage;
    }

}
